package cn.itcast.nio.c4;



import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class WriteServer {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);

        Selector selector= Selector.open();
        ssc.register(selector, SelectionKey.OP_ACCEPT);

        ssc.bind(new InetSocketAddress(8080));

        while(true){
            selector.select();
            Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
            while(iter.hasNext()){
                SelectionKey key = iter.next();
                iter.remove();
                if(key.isAcceptable()){
                    SocketChannel sc = ssc.accept();
                    sc.configureBlocking(false);
                    SelectionKey scKey = sc.register(selector, 0, null);

                    //1.向客户端发送大数据
                    StringBuilder sb=new StringBuilder();
                    for(int i=0;i<3000000;i++){
                        sb.append("a");
                    }
                    ByteBuffer buffer= Charset.defaultCharset().encode(sb.toString());
                    //2.返回值代表实际写入的字节数
                    int write=sc.write(buffer);
                    System.out.println(write);
                    //3.判断是否有剩余事件
                    if(buffer.hasRemaining()){
                        scKey.interestOps(scKey.interestOps()+SelectionKey.OP_WRITE);
                        scKey.attach(buffer);
                    }
                }
            }
        }
    }
}
